<template>
  <el-row class="logon-big">
    <el-row class="logon-box">
      <el-col :span="12" class="logon-box">
        <el-col :span="12">
          <img src="../../public/img/icons/logon.png" alt="">
        </el-col>
        <el-col :span="12">
          <el-col :span="4"></el-col>
          <el-col :span="16" class="center-container">
            <h2>后台管理系统</h2>
            <p>MANAGEMENT SYSTEM</p>

            <el-form ref="form" :model="form" :rules="rules">
              <el-form-item prop="username">
                <el-input v-model="form.username" placeholder="用户名" prefix-icon="el-icon-user" />
              </el-form-item>

              <el-form-item prop="password">
                <el-input v-model="form.password" placeholder="密码" type="password" prefix-icon="el-icon-lock"
                  show-password />
              </el-form-item>

              <el-button type="primary" style="width: 208px;" :loading="loading" @click="handleLogin">
                登录
              </el-button>
            </el-form>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </el-row>
</template>

<script>

export default {
  mixins: [http()],
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.form.validate()
        this.loading = true

        const res = await this.$httpPost('/login', this.form)
        localStorage.setItem('token', res.data.token)
        this.$router.push('/list')
        this.$message.success('登录成功')
      } catch (error) {
        this.$handleError(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="scss">
/* 保留原有样式不变 */
* {
  margin: 0;
  padding: 0;
}

.logon-big {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.logon-box {
  width: 1000px;
  height: 700px;
  background-color: white;
}

.logon-box img {
  width: 100%;
  height: 700px;
}

.center-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 600px;
  width: 300px;
  margin-left: 50px;
}

.center-container {
  width: 300px;

  el-input {
    width: 100px;
  }
}

.center-container .username-input {
  text-align: left;
  margin-bottom: 20px;
  margin-top: 30px;

  p {
    margin-top: 5px;
    color: red;
  }
}

.center-container .password-input {
  text-align: left;
  margin-bottom: 20px;

  p {
    color: red;
    margin-top: 5px;
  }
}
</style>
